package net.slenderloris.TouchServer;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.font.FontRenderContext;
import java.awt.font.TextLayout;
import java.awt.geom.Rectangle2D;
import java.util.LinkedList;


public class FingerCanvas extends Canvas {

	private static final int MAX_FINGERS = 20;

	private Point[] mPoints = new Point[MAX_FINGERS];
	
	private Color[] mColours = {
			new Color(0xFFFF0000),
			new Color(0xFF00FF00),
			new Color(0xFF0000FF),
			new Color(0xFFF0F000),
			new Color(0xFF0F0F00),
			new Color(0xFF00F0F0),
			new Color(0xFF000F0F),
			new Color(0xFFF0F0F0),
			new Color(0xFF0F0F0F),
			new Color(0xFFFF0000),
			new Color(0xFF00FF00),
			new Color(0xFF0000FF),
			new Color(0xFFF0F000),
			new Color(0xFF0F0F00),
			new Color(0xFF00F0F0),
			new Color(0xFF000F0F),
			new Color(0xFFF0F0F0),
			new Color(0xFF0F0F0F),
			new Color(0xFFFF0000),
			new Color(0xFF00FF00),
	};

	private Point mTapPoint;

	private long mLastRepaintTime;

	private KeyboardTrainer mKeyboardTrainer;

	private String mTypedText;

	private Font mFont;

	private FontRenderContext mFontRenderContext;
	
	public FingerCanvas() {
		mFont = new Font(Font.DIALOG, Font.BOLD, 40);
		mFontRenderContext = new FontRenderContext(null, false, false);

	}
	
	public void setKeyboardTrainer(KeyboardTrainer keyboardTrainer) {
		mKeyboardTrainer = keyboardTrainer;
	}
	
	public void setTypedText(String typedText) {
		mTypedText = typedText;
	}

	public synchronized void setPosition(int id, int x, int y) {
		
		if(id>=MAX_FINGERS) {
			return;
		}
		
		Point p = mPoints[id];
		if(p==null) {
			p = new Point(x,y);
			mPoints[id] = p;
		}
		else {
			p.x = x;
			p.y = y;
		}
		
		requestRepaint();
	}
	
	public void fingerUp(int id) {
		Point p = mPoints[id];
		if(p!=null) {
			p.x = -1;
		}
		requestRepaint();
	}
	
	private void requestRepaint() {
		if(mLastRepaintTime==-1) {
			repaint();
		}
		else {
			long timeNow = System.currentTimeMillis();
			long dt = timeNow-mLastRepaintTime;
			if(dt>33) {
				repaint();
			}
		}
	}

	@Override
	public void paint(Graphics g) {

		if(mTapPoint!=null) {
			g.setColor(new Color(0xFF804040));
			g.fillOval(mTapPoint.x-20, mTapPoint.y-20, 40, 40);
		}
		
		for(int i=0; i<MAX_FINGERS; i++) {
			Point p = mPoints[i];
			if(p!=null && p.x!=-1) {
				g.setColor(mColours[i]);
				g.fillOval(p.x-10, p.y-10, 20, 20); 
			}
		}
		
		
		g.setFont(mFont);
		
		if(mKeyboardTrainer!=null) {
			for(int i=0; i<26; i++) {
				KeyboardTrainer.Key k = mKeyboardTrainer.getKey(i);
				Point p = k.getAveragePosition();
				if(p!=null) {
					if(k.mLetter==mKeyboardTrainer.getCurrentLetter()) {
						g.setColor(new Color(0xFFFF0000));
					} else {
						g.setColor(new Color(0xFF000000));
					}
					
					String str = Character.toString(k.mLetter);
					TextLayout tl = new TextLayout(str, mFont, mFontRenderContext);
					Rectangle2D bounds = tl.getBounds();
					int halfw = (int) (bounds.getWidth()/2);
					int halfh = (int) (bounds.getHeight()/2);
					g.drawString(str, p.x-halfw, p.y+halfh);
				}
			}
		}
		
		if(mTypedText!=null) {
			g.setColor(new Color(0xFF0000FF));
			g.drawString(mTypedText, 100, 100);
		}
		
		mLastRepaintTime = System.currentTimeMillis();
		
	}

	public void setTapPoint(int x, int y) {
		mTapPoint = new Point(x,y);
		repaint();
	}
	
}
